#include "common.h"
#include "update.h"
#include "server.h"
#include "usermanager.h"
#include "timer.h"
#include "part.h"
#include "alarm.h"
#include "config.h"
#include "main.h"
#include "log.h"
#include "opm.h"
#include "oxc.h"
#include "wss.h"

void init_struct();
void init_thread();

void feed_dog(void* param){
    int fd = *(int*)param;
    ioctl(fd,WDIOC_KEEPALIVE,0);
}

void start_watchdog(int timeout){
    int fd = open("/dev/watchdog",O_WRONLY);
    ioctl(fd,WDIOC_SETTIMEOUT, &timeout);
    timer_start(feed_dog,timeout*10,&fd,1);
}

void init(void){
    init_struct();
    init_thread();
    // start_watchdog(10);
}

void init_struct(){
    init_config();
    init_log();
    init_update();
    init_usermanager();
    init_alarm();    
}

void init_thread(){
    pthread_t th;
    int pos_in = 0,pos_out = 1;
    pthread_create(&th,NULL,server_thread,NULL);
    pthread_create(&th,NULL,timer_thread,NULL);
    pthread_create(&th,NULL,opm_thread,(void*)pos_in);
    pthread_create(&th,NULL,opm_thread,(void*)pos_out);
    pthread_create(&th,NULL,wss_thread,NULL);
    pthread_create(&th,NULL,oxc_thread,NULL);
}

void tty_test(){
    char tty_no[32] = {0};
    int fd[8];
    int i,ret;
    char buff[1024] = {0};
    static device d[8] = {0};
    static int init=0;

    if(init == 0){
        for(i=1;i<8;i++){
            sprintf(tty_no,"/dev/ttymxc%d",i);
            fd[i] = open_uart(tty_no,115200,8,'N',1);
            d[i].uart_fd = fd[i];
        }
        init = 1;
    }
    for(i=1;i<8;i++){
        sprintf(buff,"hello ttymxc%d\n",i);
        ret = query_tty(&d[i],buff,strlen(buff),buff,1024,0,500);
        if(ret > 0){
            buff[ret] = 0;
            printf("recv ttymxc%d's data: %s\n\n",i,buff);
        }
    }        
}

void show_sysinfo(){
    char date[64] = {0};
    printf("ver is %s\n",main_firmware_version);  
    get_exact_time(date);
    printf("date is:%s\n",date);
}

int main(){   
    show_sysinfo();
    init();   
    while(1){
        sleep(1);
    };
    return 0;
}
  

#if 0
void copyfile(char* srcfile,char* dstfile){
    uint8_t buf[1024];
    int len,ret;
    FILE* fps,*fpd;
    if((fps = fopen(srcfile,"rb")) == NULL) return;
    if((fpd = fopen(dstfile,"wb+")) == NULL) return;
    len = fread(buf,1,1024,fps);
    while(len > 0){       
        fwrite(buf,1,len,fpd);
        len = fread(buf,1,1024,fps);
    }
    fclose(fps);
    fclose(fpd);
}

#define COUNT 10
int main(){
    char* pattern = "([0-9]+,?){1,}";
    int i,z,cflags=REG_EXTENDED|REG_ICASE;
    char buf[] = "124,251,252,125,1,125,1251";
    char ebuf[256] = {0};
    regex_t reg;
    regmatch_t match[COUNT];
    size_t nmatch = COUNT;

    z = regcomp(&reg,pattern,cflags);
    if(z != REG_NOERROR){
        regerror(z,&reg,ebuf,sizeof(ebuf));
        printf("1 %s:pattern '%s' \n",ebuf,pattern);
        return 0;
    }

    z = regexec(&reg,buf,nmatch,match,0);
    if(z != REG_NOERROR){
        regerror(z,&reg,ebuf,sizeof(ebuf));
        printf("%s : pattern '%s' \n",ebuf,pattern);
        regfree(&reg);
        return 0;
    }
    
    for(i=0;i<COUNT;i++){
        // if(match[i].rm_so != -1){
            memcpy(ebuf,&buf[match[i].rm_so],match[i].rm_eo - match[i].rm_so);
            ebuf[match[i].rm_eo - match[i].rm_so] = 0;
            printf("%d start = %d,end = %d,value = %s\n",i,match[i].rm_so,match[i].rm_eo,ebuf);
        // }
    }
    regfree(&reg);
    return 0;
}


int main(){
    int i,len;
    char value;
    cmdquene* quene = NULL;
    cmd c,*pcmd = NULL;
    initquene(&quene,5);
    printf("size=%d,rear=%d,front=%d\n",quene->size,quene->rear,quene->front);
    for(i=0;i<5;i++){
        pcmd = (cmd*)malloc(sizeof(cmd));
        pcmd->cmd_type = i;
        strcpy(pcmd->param_id,"123");
        value = i+'1';
        memcpy(pcmd->param_value ,&value,1);
        enquene(quene,pcmd);
        free(pcmd);
    }
    len = getquenelength(quene);
    printf("len = %d\n",len);
    for(i=0;i<len;i++){
        dequene(quene,&c);
        printf("cmd->type = %d,cmd->param_id = %s,cmd->param_value = %s\n",c.cmd_type,c.param_id,c.param_value);
    }
    destoryquene(quene);
    return 1;
}

#endif

